﻿using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Blog.UI.Infra.AutoMapper;
using Xango.Data.NHibernate.Configuration;

namespace Blog.UI
{
    public class MvcApplication : HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            routes.MapRoute(
                "PostPaginated", // Route name
                "Posts/{itemsPerPage}/{pageNumber}", // URL with parameters
                new { controller = "Posts", action = "Index" },
                new
                {
                    itemsPerPage = @"\d+",
                    pageNumber = @"\d+",
                    httpMethod = new HttpMethodConstraint("GET")
                });

            routes.MapRoute(
                "CommentPost", // Route name
                "posts/{slug}/comment", // URL with parameters
                new { controller = "Posts", action = "Comment" }, // Parameter defaults
                new { slug = @"[\w\-]+" }
                );

            routes.MapRoute(
                "Delete", // Route name
                "{controller}/{id}", // URL with parameters
                new { action = "Delete" }, // Parameter defaults
                new { httpMethod = new HttpMethodConstraint("DELETE") }
                );

            routes.MapRoute(
                "Create", // Route name
                "{controller}/Create", // URL with parameters
                new { action = "Create" } // Parameter defaults
                );

            routes.MapRoute(
                "Update", // Route name
                "{controller}/{id}/Edit", // URL with parameters
                new { action = "Edit", httpMethod = new HttpMethodConstraint("POST") }
                );

            routes.MapRoute(
                "Edit", // Route name
                "{controller}/{id}/Edit", // URL with parameters
                new { action = "Edit", httpMethod = new HttpMethodConstraint("GET") }
                );

            routes.MapRoute(
            "RecentPosts", // Route name
            "RecentPosts", // URL with parameters
            new { action = "Last", controller = "Posts", httpMethod = new HttpMethodConstraint("GET") }
            );

            routes.MapRoute(
                "Details", // Route name
                "{controller}/{id}", // URL with parameters
                new { action = "Details" }
                );

            routes.MapRoute(
                "Root", // Route name
                "", // URL with parameters
                new { controller = "Posts", action = "Index" }
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
        }

        protected void Application_Start()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            AreaRegistration.RegisterAllAreas();

            AutoMapperConfiguration.Configure();

            Application["_nhSessionFactory"] = NHFluentConfiguration.SessionFactory;

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}